package com.knife.message.domain.service.impl;

import com.knife.common.exception.CommonException;
import com.knife.message.domain.entity.KnifeMessage;
import com.knife.message.domain.entity.KnifeMessageTemplate;
import com.knife.message.domain.entity.KnifeUserMessage;
import com.knife.message.domain.service.KnifeEmailSendHelper;
import com.knife.message.domain.service.KnifeMessageBuilder;
import com.knife.message.domain.service.KnifeMessageSendHelper;
import com.knife.message.infra.cache.v2.KnifeMessageTemplateCache;
import com.knife.message.infra.mapper.KnifeMessageMapper;
import com.knife.message.infra.mapper.KnifeUserMessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

/**
 * @author tianhao.luo@hand-china.com
 * @version 1.0
 * @date 2022年06月12日 星期日 5:25 下午
 * @description
 * @since 1.0
 */
@Component
@SuppressWarnings("ALL")
public class DefaultKnifeMessageSendHelper implements KnifeMessageSendHelper {
    @Autowired
    private KnifeEmailSendHelper knifeEmailSendHelper;
    @Autowired
    private KnifeMessageBuilder knifeMessageBuilder;
    @Autowired
    private KnifeMessageTemplateCache knifeMessageTemplateCache;

    @Resource
    private KnifeMessageMapper knifeMessageMapper;
    @Resource
    private KnifeUserMessageMapper knifeUserMessageMapper;

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void sendEmail(String code, Map<String, String> subjectMap, Map<String, String> contentMap, List<Long> receiverList, Function<Long, String> emailFunction) {
        KnifeMessageTemplate knifeMessageTemplate = new KnifeMessageTemplate();
        knifeMessageTemplate.setCode(code);
        KnifeMessageTemplate value = knifeMessageTemplateCache.getCache(knifeMessageTemplate);
        String content = knifeMessageBuilder.buildMessage(value.getContent(), contentMap);
        String subject = knifeMessageBuilder.buildMessage(value.getSubject(), subjectMap);
        // 记录本次发送的邮件信息
        KnifeMessage knifeMessage = new KnifeMessage();
        knifeMessage.setTemplateId(value.getTemplateId());
        knifeMessage.setContent(content);
        knifeMessage.setSubject(subject);
        knifeMessageMapper.insert(knifeMessage);
        // 获取接收人邮箱
        receiverList.forEach(receiver -> {
            // 记录本次接收人
            KnifeUserMessage knifeUserMessage = new KnifeUserMessage();
            knifeUserMessage.setMessageId(knifeMessage.getMessageId());
            knifeUserMessage.setUserId(receiver);
            knifeUserMessage.setSendFlag(1);
            try {
                knifeEmailSendHelper.sendEmail(subject, content, emailFunction.apply(receiver));
            } catch (Exception e) {
                knifeUserMessage.setSendFlag(0);
                knifeUserMessage.setAttribute1(e.getMessage());
            }
            knifeUserMessageMapper.insert(knifeUserMessage);
        });
    }

    @Override
    public void sendEmailWithAttachment(String code, Map<String, String> subjectMap, Map<String, String> contentMap, List<Long> receiverList, Function<Long, String> emailFunction, List<File> fileList) {
        KnifeMessageTemplate knifeMessageTemplate = new KnifeMessageTemplate();
        knifeMessageTemplate.setCode(code);
        KnifeMessageTemplate value = knifeMessageTemplateCache.getCache(knifeMessageTemplate);
        String content = knifeMessageBuilder.buildMessage(value.getContent(), contentMap);
        String subject = knifeMessageBuilder.buildMessage(value.getSubject(), subjectMap);
        // 记录本次发送的邮件信息
        KnifeMessage knifeMessage = new KnifeMessage();
        knifeMessage.setTemplateId(value.getTemplateId());
        knifeMessage.setContent(content);
        knifeMessage.setSubject(subject);
        knifeMessageMapper.insert(knifeMessage);
        // 获取接收人邮箱
        receiverList.forEach(receiver -> {
            // 记录本次接收人
            KnifeUserMessage knifeUserMessage = new KnifeUserMessage();
            knifeUserMessage.setMessageId(knifeMessage.getMessageId());
            knifeUserMessage.setUserId(receiver);
            knifeUserMessage.setSendFlag(1);
            try {
                knifeEmailSendHelper.sendEmailWithAttachment(subject, content, emailFunction.apply(receiver), fileList);
            } catch (Exception e) {
                knifeUserMessage.setSendFlag(0);
                knifeUserMessage.setAttribute1(e.getMessage());
            }
            knifeUserMessageMapper.insert(knifeUserMessage);
        });
    }

    @Override
    public void sendEmail(String code, Map<String, String> subjectMap, Map<String, String> contentMap, String email) {
        KnifeMessageTemplate knifeMessageTemplate = new KnifeMessageTemplate();
        knifeMessageTemplate.setCode(code);
        KnifeMessageTemplate value = knifeMessageTemplateCache.getCache(knifeMessageTemplate);
        String content = knifeMessageBuilder.buildMessage(value.getContent(), contentMap);
        String subject = knifeMessageBuilder.buildMessage(value.getSubject(), subjectMap);
        // 记录本次发送的邮件信息
        KnifeMessage knifeMessage = new KnifeMessage();
        knifeMessage.setTemplateId(value.getTemplateId());
        knifeMessage.setContent(content);
        knifeMessage.setSubject(subject);
        knifeMessageMapper.insert(knifeMessage);
        // 记录本次接收人
        try {
            knifeEmailSendHelper.sendEmail(subject, content, email);
        } catch (Exception e) {
            throw new CommonException("邮件发送失败!");
        }
    }

    @Override
    public void sendEmailWithAttachment(String code, Map<String, String> subjectMap, Map<String, String> contentMap, String email, List<File> fileList) {
        KnifeMessageTemplate knifeMessageTemplate = new KnifeMessageTemplate();
        knifeMessageTemplate.setCode(code);
        KnifeMessageTemplate value = knifeMessageTemplateCache.getCache(knifeMessageTemplate);
        String content = knifeMessageBuilder.buildMessage(value.getContent(), contentMap);
        String subject = knifeMessageBuilder.buildMessage(value.getSubject(), subjectMap);
        // 记录本次发送的邮件信息
        KnifeMessage knifeMessage = new KnifeMessage();
        knifeMessage.setTemplateId(value.getTemplateId());
        knifeMessage.setContent(content);
        knifeMessage.setSubject(subject);
        knifeMessageMapper.insert(knifeMessage);
        // 记录本次接收人
        try {
            knifeEmailSendHelper.sendEmailWithAttachment(subject, content, email, fileList);
        } catch (Exception e) {
            throw new CommonException("邮件发送失败!");
        }
    }
}
